//
//  CC_Classify_CollectionViewCell.m
//  HimalayaMountain
//
//  Created by dllo on 16/7/15.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CC_Classify_CollectionViewCell.h"
#import "CCBaseCollectionView.h"

#import "CC_ClassifyHugeImage_CollectionViewCell.h"
#import "CC_ClassifyImageLabel_CollectionViewCell.h"
#import "CC_ClassifyCollectionView_CollectionViewCell.h"

#import "CC_Classify_Model.h"

#define HugeImage @"CC_ClassifyHugeImage_CollectionViewCell"
#define ImageLabelH @"CC_ClassifyImageLabel_CollectionViewCell"
#define VDISCOVERCELL @"CC_ClassifyCollectionView_CollectionViewCell"


@interface CC_Classify_CollectionViewCell ()<UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout>

@property(nonatomic, retain) CCBaseCollectionView *collectionView;
@property(nonatomic, retain) NSArray *classifyModelArray;


// 暂时试验用
@property(nonatomic, retain) NSMutableArray *twoArray;
@property(nonatomic, retain) NSMutableArray *threeArray;
@property(nonatomic, retain) NSMutableArray *fourArray;
@property(nonatomic, retain) NSMutableArray *fiveArray;
@property(nonatomic, retain) NSMutableArray *sixArray;

@end



@implementation CC_Classify_CollectionViewCell

- (void)dealloc
{
    [_classifyModelArray release];
    [_collectionView release];
    [super dealloc];
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self handleData];
        [self createCollectionView];
        
    }
    return self;
}

-(void)handleData{
    self.classifyModelArray = [NSArray array];
    
    self.twoArray = [NSMutableArray array];
    self.threeArray = [NSMutableArray array];
    self.fourArray = [NSMutableArray array];
    self.fiveArray = [NSMutableArray array];
    self.sixArray = [NSMutableArray array];

    

    
    
    [CCNetTool GET:@"http://mobile.ximalaya.com/mobile/discovery/v2/categories?channel=ios-b1&code=43_210000_2102&device=iPhone&picVersion=13&scale=2&version=4.3.98" body:nil headerFile:nil response:CCJSON success:^(id result) {
//        NSLog(@"_____________________%@", result);
        NSDictionary *dic = result;
        NSArray *list = [dic objectForKey:@"list"];
        self.classifyModelArray = [CC_Classify_Model arrayFromArray:list];
        
//        self.twoArray = [self.classifyModelArray ]
        for (int i = 1; i <= 6; i ++) {
            [self.twoArray addObject:[self.classifyModelArray objectAtIndex:i]];
        }
        for (int i = 7; i <= 12; i ++) {
            [self.threeArray addObject:[self.classifyModelArray objectAtIndex:i]];
        }
        for (int i = 13; i <= 18; i ++) {
            [self.fourArray addObject:[self.classifyModelArray objectAtIndex:i]];
        }
        for (int i = 19; i <= 24; i ++) {
            [self.fiveArray addObject:[self.classifyModelArray objectAtIndex:i]];
        }
        for (int i = 25; i <= 28; i ++) {
            [self.sixArray addObject:[self.classifyModelArray objectAtIndex:i]];
        }
        
        
        
        
        
        
        [self.collectionView reloadData];
        
    } failure:^(NSError *error) {
        NSLog(@"______________ERROR%@", error);
        
    }];
    
    
    
}

-(void)createCollectionView{
    UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc]init];
    
    
    flowLayout.sectionInset = UIEdgeInsetsMake(0, 0, 0, 0);

    flowLayout.minimumInteritemSpacing = 0;
    flowLayout.minimumLineSpacing = 0;

    self.collectionView = [[CCBaseCollectionView alloc]initWithFrame:CGRectMake(0, 0, SCREENWIDTH, SCREENHEIGHT - 64 - 40 - 49) collectionViewLayout:flowLayout];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];  // 颜色
    
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.contentView addSubview:_collectionView];
    [flowLayout release];
    [_collectionView release];

    
    // 注册尾视图重用池
    [self.collectionView registerClass:[UICollectionReusableView class] forSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer"];
  // 注册重用池
    
    [self.collectionView registerClass:[CC_ClassifyHugeImage_CollectionViewCell class] forCellWithReuseIdentifier:HugeImage];
    
    [self.collectionView registerClass:[CC_ClassifyCollectionView_CollectionViewCell class] forCellWithReuseIdentifier:VDISCOVERCELL];
    
    [self.collectionView registerClass:[CC_ClassifyImageLabel_CollectionViewCell class] forCellWithReuseIdentifier:ImageLabelH];
    
}

// 区数
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    
    return 7;
    
    
}


// 每个区item的数
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
//    if (section <= 1) {
//        return 1;
//    }else if(section > 1 && section < 6) {
//        return 6;
//    }else{
//        return 4;
//    }
    
    switch (section) {
        case 0:
        {
            return 1;
        }
            break;
        case 1:
        {
            return 1;
        }
            break;
        case 2:
        {
            return self.twoArray.count;
        }
            break;
        case 3:
        {
            return self.threeArray.count;
        }
            break;
        case 4:
        {
            return self.fourArray.count;
        }
            break;
        case 5:
        {
            return self.fiveArray.count;
        }
            break;
//        case 6:
//        {
//            return self.sixArray.count;
//        }
            break;
            
        default:
        {
            return self.sixArray.count;
        }
            break;
    }
    
    
    
    
}

// 每个区itemsize的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    
    if (indexPath.section == 0 )
    {
        
        return CGSizeMake(SCREENWIDTH , SCREENHEIGHT * 0.3);  // 图片大小
        
    }else if (indexPath.section == 1)
    {
        return CGSizeMake(SCREENWIDTH , SCREENHEIGHT / 7);
        
    }else
    {
        
        return CGSizeMake(SCREENWIDTH  / 2, SCREENHEIGHT * 0.25 / 3);
        
    }
    
    
    
}

// 区尾大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section{
    if(section == 0){
        return CGSizeMake(0, 0);
    }else{
    return CGSizeMake(SCREENWIDTH, 15);
    }
}


/// 返回尾部视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

        UICollectionReusableView *footerView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"footer" forIndexPath:indexPath];
        
        footerView.backgroundColor = [UIColor grayColor];  // 区尾颜色
        footerView.alpha = 0.3;
        return footerView;
    
}





-(UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    switch (indexPath.section) {
        case 0:
        {   CC_ClassifyHugeImage_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:HugeImage forIndexPath:indexPath];
//            cell.backgroundColor = [UIColor whiteColor];
            cell.classifyModel = self.classifyModelArray.firstObject;
            
            return cell;
        }
            break;
        case 1:
        {
            
    CC_ClassifyCollectionView_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:VDISCOVERCELL forIndexPath:indexPath];
    cell.contentView.backgroundColor = [UIColor whiteColor];
    return cell;

        }
            break;
        case 2:
        {
            CC_ClassifyImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageLabelH forIndexPath:indexPath];
            
            cell.classifyModel = [self.twoArray objectAtIndex:indexPath.row ];
             return cell;
        }
            break;
        case 3:
        {
            CC_ClassifyImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageLabelH forIndexPath:indexPath];
            cell.classifyModel = [self.threeArray objectAtIndex:indexPath.row ];
             return cell;
        }
            break;
        case 4:
        {
            CC_ClassifyImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageLabelH forIndexPath:indexPath];
            cell.classifyModel = [self.fourArray objectAtIndex:indexPath.row ];
             return cell;
        }
            break;
        case 5:
        {
            CC_ClassifyImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageLabelH forIndexPath:indexPath];
            cell.classifyModel = [self.fiveArray objectAtIndex:indexPath.row ];
             return cell;
            
        }
            break;
            
            
        default:
        {
            CC_ClassifyImageLabel_CollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:ImageLabelH forIndexPath:indexPath];
            cell.classifyModel = [self.sixArray objectAtIndex:indexPath.row];
            
            return cell;
        }
            break;
    }
    
    
    
    
}





@end
